<?php

ob_start();

//if ($_SERVER['HTTP_HOST'] != 'localhost')
//die('เธ�เธดเธ”เธ�เธฐ');
require '../libraries/master.php';

$request = isset($_GET['q'])?$_GET['q']:'';

if (empty($request)) $request = 'dashboard';
$request = explode('/', $request);

$action = '';

if ($request[0] == 'admin') {
	$action = 'admin_';
	array_shift($request);
	requireAdmin();
}

$moduleClassName = ucFirst($request[0]) . 'Module';

if (!class_exists($moduleClassName, true)) {
	render404();
}

$module = new $moduleClassName;
$action .= isset($request[1]) ? $request[1] : 'index';

$argv = array();

if (count($request) > 2) {
	$argv = $request;
	array_shift($argv);
	array_shift($argv);
}

if (method_exists($module, $action)) {
	$module->$action($argv);
} else {
	if ($action == 'index') render404();
	elseif (method_exists($module, 'index')) {
		$module->index($argv);
	} else {
		render404();
	}
}

$module->_output();

function render404() {
	header('HTTP/1.1 404 Not Found');
	$views = new Views();
	$views->addContent('404');
	$views->render();
	die;
}
